#include "SlopeTile.h"
#include "RenderManager.h"

SlopeTile::SlopeTile(unsigned short tileSize, sf::Vector2f& position, sf::Texture& tile, short type) : Tile(tileSize, position, tile, type)
{
	sf::Image image = tile.copyToImage();
	unsigned short previousX = tileSize;
	sf::Color color;
	unsigned short xCheck = 0;
	unsigned short xCheck2 = 0;
	for (unsigned short y = 0; y < tileSize; y++)
	{
		// Hacky solution for creating seperate collision boxes for each step in the slope
		for (unsigned short x = 0; x < tileSize; x++)
		{
			color = image.getPixel(x, y);
			if (color.a == 0)
				continue;
			else
			{
				
				bool value = true;
				unsigned short i = x;
				for (; i < previousX; i++)
				{
					color = image.getPixel(i, y);
					if (color.a == 0)
					{

						value = false;
						break;
					}
				}
				_collisionData.push_back(sf::FloatRect(_base.getPosition().x + static_cast<float>(x) + xCheck, _base.getPosition().y + static_cast<float>(y), static_cast<float>((i - x) - xCheck), static_cast<float>(tileSize - y)));
				if (value)
					previousX = x;
				else
				{
					xCheck2 = i;
					xCheck = i;
				}
				break;
			}
		}
	}
}

SlopeTile::~SlopeTile(void){}


void SlopeTile::draw(RenderManager* renderManager)
{
	for (auto it = _collisionData.begin(); it != _collisionData.end(); it++)
	{
		sf::RectangleShape rect(sf::Vector2f((*it).width, (*it).height));
		rect.setPosition(sf::Vector2f((*it).left, (*it).top));
		rect.setFillColor(sf::Color::Cyan);
		renderManager->draw(rect);

	}
}